/********************************************************************************
 * FrontBaseSupport.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 26, 2008 11:07:23 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.SearchServiceDelegator;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.Itemtype;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.dto.LocationCategoriesWOS;

public class FrontBaseSupport extends ActionSupport{

	/**  */
	private static final long serialVersionUID = 1L;

	private boolean loginPage = false;

	private boolean leftAllowed = true;

	private boolean rightAllowed = true;

	private boolean categoryDisplay = false;

	private boolean myAccount = false;

	private boolean index = false;

	private int totalType = 0;

	private int totalCategories = 0;

	private int totalLocations = 0;

	private int totalLanguages = 0;

	private List<Itemtype> itemTypeSearch = getAllType();

	private List<GeneralCategoriesWOS> generalCategoriesSearch = getAllCategories();

	private List<LocationCategoriesWOS> locationCategoriesSearch = getAllLocation();

	private List<Languages> languagesSearch = getAllLanguages();

	public String configurationUrl = new String();

	private String logo = "top";

	private String categoryHeader = "category.menu.allForSale";

	public boolean isMyAccount() {
		return myAccount;
	}

	public void setMyAccount(boolean myAccount) {
		this.myAccount = myAccount;
	}

	/**
	 * @return the categoryDisplay
	 */
	public boolean isCategoryDisplay() {
		return categoryDisplay;
	}

	/**
	 * @param categoryDisplay the categoryDisplay to set
	 */
	public void setCategoryDisplay(boolean categoryDisplay) {
		this.categoryDisplay = categoryDisplay;
	}

	public FrontBaseSupport(boolean leftAllowed, boolean rightAllowed) {
		this.leftAllowed = leftAllowed;
		this.rightAllowed = rightAllowed;
	}

	public boolean isLeftAllowed() {
		return leftAllowed;
	}

	public void setLeftAllowed(boolean leftAllowed) {
		this.leftAllowed = leftAllowed;
	}

	public boolean isRightAllowed() {
		return rightAllowed;
	}

	public void setRightAllowed(boolean rightAllowed) {
		this.rightAllowed = rightAllowed;
	}

	public boolean isLoginPage() {
		return loginPage;
	}

	public void setLoginPage(boolean loginPage) {
		this.loginPage = loginPage;
	}

	public String getCategoryHeader() {
		return categoryHeader;
	}

	public void setCategoryHeader(String categoryHeader) {
		this.categoryHeader = categoryHeader;
	}

	public boolean isIndex() {
		return index;
	}

	public void setIndex(boolean index) {
		this.index = index;
	}

	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getConfigurationUrl() {
		return configurationUrl;
	}

	public void setConfigurationUrl(String configurationUrl) {
		this.configurationUrl = configurationUrl;
	}

	private List<Itemtype> getAllType() {
		List<Itemtype> tmpList = (List<Itemtype>) CommonFunctionDelegator.getAllfromtable(Itemtype.class.getName());

		for (Itemtype itemtype : tmpList) {
			itemtype.setCount(SearchServiceDelegator.getItemCountFor(itemtype.getId(), 0, 0, "0"));
			totalType += itemtype.getCount();
		}

		return  tmpList;
	}

	private List<GeneralCategoriesWOS> getAllCategories() {
		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("level", 1));
		criterionList.add(Restrictions.eq("active", true));
		List<GeneralCategoriesWOS> tmpList = (List<GeneralCategoriesWOS>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, GeneralCategoriesWOS.class);

		for (GeneralCategoriesWOS generalCategoriesWOS : tmpList) {
			List list = CommonFunctionDelegator.executeSQLQuery("SELECT count(*) FROM items i, general_categories g, item_categories ig where i.itemId = ig.itemId And g.categoryId = ig.categoryId And g.mainparentid = " + generalCategoriesWOS.getMainParentId());
			Object objects = (Object) list.iterator().next();
			generalCategoriesWOS.setCount(Integer.parseInt(objects.toString()));
			totalCategories += generalCategoriesWOS.getCount();
		}

		return  tmpList;
	}

	private List<LocationCategoriesWOS> getAllLocation() {
		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("level", 1));
		List<LocationCategoriesWOS> tmpList = (List<LocationCategoriesWOS>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, LocationCategoriesWOS.class);

		for (LocationCategoriesWOS locationCategoriesWOS : tmpList) {
			locationCategoriesWOS.setCount(SearchServiceDelegator.getItemCountFor(0, 0, locationCategoriesWOS.getCategoryId(), "0"));
			totalLocations += locationCategoriesWOS.getCount();
		}

		return  tmpList;
	}

	private List<Languages> getAllLanguages() {
		List<Criterion> criterionList = new ArrayList<Criterion>();
		criterionList.add(Restrictions.eq("active", true));
		List<Languages> tmpList = (List<Languages>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Languages.class);

		for (Languages language : tmpList) {
			language.setCount(SearchServiceDelegator.getItemCountFor(0, 0, 0, language.getLanguageCode()));
			totalLanguages += language.getCount();
		}

		return tmpList;
	}

	public List<GeneralCategoriesWOS> getGeneralCategoriesSearch() {
		return generalCategoriesSearch;
	}

	public List<LocationCategoriesWOS> getLocationCategoriesSearch() {
		return locationCategoriesSearch;
	}

	public List<Languages> getLanguagesSearch() {
		return languagesSearch;
	}

	public List<Itemtype> getItemTypeSearch() {
		return itemTypeSearch;
	}

	public int getTotalType() {
		return totalType;
	}

	public int getTotalCategories() {
		return totalCategories;
	}

	public int getTotalLocations() {
		return totalLocations;
	}

	public int getTotalLanguages() {
		return totalLanguages;
	}
}
